package com.whz.governance.http.ribbon.rule;

import com.netflix.loadbalancer.AbstractServerPredicate;
import com.netflix.loadbalancer.AvailabilityPredicate;
import com.netflix.loadbalancer.CompositePredicate;
import com.netflix.loadbalancer.PredicateBasedRule;
import org.springframework.util.Assert;

/**
 * @author whz
 */
public abstract class AbstractDiscoveryEnabledRule extends PredicateBasedRule {

    private final CompositePredicate predicate;

    protected AbstractDiscoveryEnabledRule(AbstractServerPredicate discoveryEnabledPredicate) {
        Assert.notNull(discoveryEnabledPredicate, "Parameter 'discoveryEnabledPredicate' can't be null");
        predicate = createCompositePredicate(discoveryEnabledPredicate, new AvailabilityPredicate(this, null));
    }

    @Override
    public AbstractServerPredicate getPredicate() {
        return predicate;
    }

    private static CompositePredicate createCompositePredicate(AbstractServerPredicate discoveryEnabledPredicate,
                                                               AvailabilityPredicate availabilityPredicate) {
        return CompositePredicate.withPredicates(discoveryEnabledPredicate, availabilityPredicate)
                                 .build();
    }
}